package net.chenlin.dp.modules.business.service.impl;

import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.common.utils.CommonUtils;
import net.chenlin.dp.modules.business.dao.LineChartMapper;
import net.chenlin.dp.modules.business.dao.TestManageMapper;
import net.chenlin.dp.modules.business.entity.dto.LineChartQueryDto;
import net.chenlin.dp.modules.business.entity.vo.*;
import net.chenlin.dp.modules.business.entity.vo.lineChart.LineChartDataVo;
import net.chenlin.dp.modules.business.entity.vo.lineChart.LineChartVo;
import net.chenlin.dp.modules.utils.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Yangsm
 * @ClassName: NewLineChartServiceImpl
 * @Description: TODO
 * @date 2023年01月11日 15:10
 */
@Service
public class NewLineChartServiceImpl {

    @Autowired
    private LineChartMapper lineChartMapper;

    @Autowired
    private TestManageMapper testManageMapper;

    // 算当前设备三相总量（不管几项：直接取总）
    public R getLineChart(LineChartQueryDto queryDto) {

        LineChartVo lineChartVo = new LineChartVo();

        List<String> electricNoList = new ArrayList<>();

        // 根据试验获取下面你的设备编号
        List<String> deviceList = testManageMapper.getDeviceList(queryDto.getTestNo());
        if(deviceList.size() == 0){
            return R.error("未获取到该实验下的设备编号！");
        }

        // 获取

        // tcp数据
        List<LineChartDataVo> typeOneList = new ArrayList<>();

        // excel数据
        List<LineChartDataVo> typeTwoList = new ArrayList<>();

        SimpleDateFormat fierformatter = new SimpleDateFormat("HH:mm:ss");

        if (deviceList.size() > 1) {
            // 多设备逻辑（最多两个设备现在）
            // 根据选取的通道信息（可能是单选或者选所有）
            // 如果是单选的

            // 获取每个设备
            LineChartQueryDto tcpQuery = new LineChartQueryDto();
            tcpQuery.setStartDate(queryDto.getStartDate());
            tcpQuery.setEndDate(queryDto.getEndDate());
            tcpQuery.setDeviceNo(deviceList.get(0));
            // 获取设备1的TCP数据
            List<DataMessageVo> d1TcpDataList = lineChartMapper.getTcpDataList(tcpQuery);
            if (d1TcpDataList.size() == 0) {
                return R.error("未获取到当前查询时间段设备1的TCP数据");
            }
            tcpQuery.setDeviceNo(deviceList.get(1));

            // 获取设备2的TCP数据
            List<DataMessageVo> d2TcpDataList = lineChartMapper.getTcpDataList(tcpQuery);

            if (d2TcpDataList.size() == 0) {
                return R.error("未获取到当前查询时间段设备2的TCP数据");
            }

            //封装TCP通道的数据
            for (int i = 0; i < d1TcpDataList.size(); i++) {

                SplicingChannelVo splicingChannelVo = new SplicingChannelVo();
                splicingChannelVo.setMeasurementStartTime(d1TcpDataList.get(i).getMeasurementStartTime());
                splicingChannelVo.setMeasurementEndTime(d1TcpDataList.get(i).getMeasurementEndTime());
                if (queryDto.getPassNo() == 1) {
                    if (i == 0) {
                        LineChartDataVo fiertVo = new LineChartDataVo();

                        String fierformat = fierformatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int index = fierformat.lastIndexOf(":");
                        fierformat = fierformat.substring(0, index);
                        fiertVo.setTime(fierformat);
                        fiertVo.setDifference("0.00");
                        typeOneList.add(fiertVo);
                    } else {
                        Double d1OnePower = d1TcpDataList.get(i).getOneChannelAccumulatedPower();
                        Double d2OnePower = d2TcpDataList.get(i).getOneChannelAccumulatedPower();

                        BigDecimal b1 = new BigDecimal(Double.toString(d1OnePower));
                        BigDecimal b2 = new BigDecimal(Double.toString(d2OnePower));
                        double res = Math.abs(b1.subtract(b2).doubleValue());

                        Double d1OnePower1 = d1TcpDataList.get(i - 1).getOneChannelAccumulatedPower();
                        Double d2OnePower2 = d2TcpDataList.get(i - 1).getOneChannelAccumulatedPower();

                        BigDecimal b11 = new BigDecimal(Double.toString(d1OnePower1));
                        BigDecimal b22 = new BigDecimal(Double.toString(d2OnePower2));
                        double res2 = Math.abs(b11.subtract(b22).doubleValue());

                        BigDecimal res3 = new BigDecimal(Double.toString(res));
                        BigDecimal res4 = new BigDecimal(Double.toString(res2));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        //splicingChannelVo.setChannelPower(result);

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);
                    }
                }
                if (queryDto.getPassNo() == 2) {
                    if (i == 0) {
                        LineChartDataVo fiertVo = new LineChartDataVo();

                        String fierformat = fierformatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int index = fierformat.lastIndexOf(":");
                        fierformat = fierformat.substring(0, index);
                        fiertVo.setTime(fierformat);
                        fiertVo.setDifference("0.00");
                        typeOneList.add(fiertVo);
                    } else {
                        Double d1OnePower = d1TcpDataList.get(i).getTwoChannelAccumulatedPower();
                        Double d2OnePower = d2TcpDataList.get(i).getTwoChannelAccumulatedPower();

                        BigDecimal b1 = new BigDecimal(Double.toString(d1OnePower));
                        BigDecimal b2 = new BigDecimal(Double.toString(d2OnePower));
                        double res = Math.abs(b1.subtract(b2).doubleValue());

                        Double d1OnePower1 = d1TcpDataList.get(i - 1).getTwoChannelAccumulatedPower();
                        Double d2OnePower2 = d2TcpDataList.get(i - 1).getTwoChannelAccumulatedPower();

                        BigDecimal b11 = new BigDecimal(Double.toString(d1OnePower1));
                        BigDecimal b22 = new BigDecimal(Double.toString(d2OnePower2));
                        double res2 = Math.abs(b11.subtract(b22).doubleValue());

                        BigDecimal res3 = new BigDecimal(Double.toString(res));
                        BigDecimal res4 = new BigDecimal(Double.toString(res2));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        //splicingChannelVo.setChannelPower(result);

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);
                    }

                }
                if (queryDto.getPassNo() == 3) {
                    if (i == 0) {
                        LineChartDataVo fiertVo = new LineChartDataVo();

                        String fierformat = fierformatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int index = fierformat.lastIndexOf(":");
                        fierformat = fierformat.substring(0, index);
                        fiertVo.setTime(fierformat);
                        fiertVo.setDifference("0.00");
                        typeOneList.add(fiertVo);
                    } else {
                        Double d1OnePower = d1TcpDataList.get(i).getThreeChannelAccumulatedPower();
                        Double d2OnePower = d2TcpDataList.get(i).getThreeChannelAccumulatedPower();

                        BigDecimal b1 = new BigDecimal(Double.toString(d1OnePower));
                        BigDecimal b2 = new BigDecimal(Double.toString(d2OnePower));
                        double res = Math.abs(b1.subtract(b2).doubleValue());

                        Double d1OnePower1 = d1TcpDataList.get(i - 1).getThreeChannelAccumulatedPower();
                        Double d2OnePower2 = d2TcpDataList.get(i - 1).getThreeChannelAccumulatedPower();

                        BigDecimal b11 = new BigDecimal(Double.toString(d1OnePower1));
                        BigDecimal b22 = new BigDecimal(Double.toString(d2OnePower2));
                        double res2 = Math.abs(b11.subtract(b22).doubleValue());

                        BigDecimal res3 = new BigDecimal(Double.toString(res));
                        BigDecimal res4 = new BigDecimal(Double.toString(res2));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        //splicingChannelVo.setChannelPower(result);

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);
                    }
                }

                System.out.println("  ======================获取设备下面的电表的电量总和===========================================");

                // 获取第一个设备的电表信息
                LineChartQueryDto newQuery = new LineChartQueryDto();
                newQuery.setTestNo(queryDto.getTestNo());
                newQuery.setDeviceNo(deviceList.get(0));
                newQuery.setPassNo(queryDto.getPassNo());

                // 根据试验编号以及设备信息以及
                String electricNo = lineChartMapper.getElectricNo(newQuery);
                if (StringUtils.isEmpty(electricNo)) {
                    return R.error("未获取到该实验设备下通道的电表号!");
                }
                String[] split = electricNo.split(",");
                List<String> threeMealsList = Arrays.asList(split);

                // 获取第、二个设备的电表信息
                newQuery.setDeviceNo(deviceList.get(1));
                // 根据试验编号以及设备信息以及
                String electricNo2 = lineChartMapper.getElectricNo(newQuery);
                if (StringUtils.isEmpty(electricNo2)) {
                    return R.error("未获取到该实验设备下通道的电表号!");
                }
                String[] split2 = electricNo2.split(",");
                List<String> threeMealsList2 = Arrays.asList(split2);

                if (i == 0) {
                    LineChartDataVo fiertVo = new LineChartDataVo();

                    String fierformat = fierformatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                    int index = fierformat.lastIndexOf(":");
                    fierformat = fierformat.substring(0, index);
                    fiertVo.setTime(fierformat);
                    fiertVo.setDifference("0.00");
                    typeTwoList.add(fiertVo);
                } else {
                    // 根据电表号以及结束时间获取该通道下面电量何
                    SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String format1 = form.format(d1TcpDataList.get(i).getMeasurementEndTime());
                    System.out.println(format1);
                    String format2 = form.format(d1TcpDataList.get(i -1).getMeasurementEndTime());
                    System.out.println(format2);

                    Double channelPower = lineChartMapper.getTimeTotal(format1, threeMealsList);
                    // 根据电表号以及结束时间获取该通道下面电量何
                    Double channelPower2 = lineChartMapper.getTimeTotal(format1, threeMealsList2);
                    if(channelPower != null && channelPower2 != null){
                        BigDecimal res3 = new BigDecimal(Double.toString(channelPower));
                        BigDecimal res4 = new BigDecimal(Double.toString(channelPower2));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        // 根据电表号以及结束时间获取该通道下面电量何
                        Double channelPower3 = lineChartMapper.getTimeTotal(format2, threeMealsList);

                        // 根据电表号以及结束时间获取该通道下面电量何
                        Double channelPower4 = lineChartMapper.getTimeTotal(format2, threeMealsList2);

                        if(channelPower3 != null && channelPower4 != null){
                            BigDecimal res5 = new BigDecimal(Double.toString(channelPower3));
                            BigDecimal res6 = new BigDecimal(Double.toString(channelPower4));
                            double result2 = Math.abs(res5.subtract(res6).doubleValue());

                            BigDecimal res7 = new BigDecimal(Double.toString(result));
                            BigDecimal res8 = new BigDecimal(Double.toString(result2));
                            double result3 = Math.abs(res7.subtract(res8).doubleValue());

                            LineChartDataVo vo = new LineChartDataVo();
                            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                            String format = formatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                            int i1 = format.lastIndexOf(":");
                            format = format.substring(0, i1);
                            vo.setTime(format);
                            String douStr = String.format("%.2f", result3);
                            if (douStr.contains("-")) {
                                douStr = douStr.replace("-", "");
                            }
                            vo.setDifference(douStr);
                            typeTwoList.add(vo);
                        }
                    }
                }
            }
        }

        // 单个设备
        if (deviceList.size() == 1) {
            LineChartQueryDto tcpQuery = new LineChartQueryDto();
            tcpQuery.setStartDate(queryDto.getStartDate());
            tcpQuery.setEndDate(queryDto.getEndDate());
            tcpQuery.setDeviceNo(deviceList.get(0));
            // 获取设备1的TCP数据
            List<DataMessageVo> tcpDataList = lineChartMapper.getTcpDataList(tcpQuery);

            //封装TCP通道的数据
            for (int i = 0; i < tcpDataList.size(); i++) {

                SplicingChannelVo splicingChannelVo = new SplicingChannelVo();
                splicingChannelVo.setMeasurementStartTime(tcpDataList.get(i).getMeasurementStartTime());
                splicingChannelVo.setMeasurementEndTime(tcpDataList.get(i).getMeasurementEndTime());

                if (i == 0) {

                    LineChartDataVo fiertVo = new LineChartDataVo();

                    String fierformat = fierformatter.format(tcpDataList.get(i).getMeasurementEndTime());
                    int index = fierformat.lastIndexOf(":");
                    fierformat = fierformat.substring(0, index);
                    fiertVo.setTime(fierformat);
                    fiertVo.setDifference("0.00");
                    typeOneList.add(fiertVo);
                    typeTwoList.add(fiertVo);

                } else {
                    if (queryDto.getPassNo() == 1) {
                        Double onePower = tcpDataList.get(i).getOneChannelAccumulatedPower();
                        Double onePower1 = tcpDataList.get(i - 1).getOneChannelAccumulatedPower();

                        BigDecimal res3 = new BigDecimal(Double.toString(onePower));
                        BigDecimal res4 = new BigDecimal(Double.toString(onePower1));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(tcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);

                    }
                    if (queryDto.getPassNo() == 2) {
                        Double onePower = tcpDataList.get(i).getTwoChannelAccumulatedPower();
                        Double onePower1 = tcpDataList.get(i - 1).getTwoChannelAccumulatedPower();

                        BigDecimal res3 = new BigDecimal(Double.toString(onePower));
                        BigDecimal res4 = new BigDecimal(Double.toString(onePower1));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(tcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);
                    }
                    if (queryDto.getPassNo() == 3) {
                        Double onePower = tcpDataList.get(i).getThreeChannelAccumulatedPower();
                        Double onePower1 = tcpDataList.get(i - 1).getThreeChannelAccumulatedPower();

                        BigDecimal res3 = new BigDecimal(Double.toString(onePower));
                        BigDecimal res4 = new BigDecimal(Double.toString(onePower1));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(tcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);
                    }
                }

                System.out.println("  ======================获取设备下面的电表的电量总和===========================================");




                if (i != 0) {
                    // 根据电表号以及结束时间获取该通道下面电量何
                    SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String format1 = form.format(tcpDataList.get(i).getMeasurementEndTime());
                    System.out.println(format1);
                    String format2 = form.format(tcpDataList.get(i -1).getMeasurementEndTime());
                    System.out.println(format2);
                    Double channelPower = lineChartMapper.getTimeTotal(format1, electricNoList);
                    Double channelPower2 = lineChartMapper.getTimeTotal(format2, electricNoList);
                    if(channelPower != null && channelPower2 != null){
                        BigDecimal res3 = new BigDecimal(Double.toString(channelPower));
                        BigDecimal res4 = new BigDecimal(Double.toString(channelPower2));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(tcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeTwoList.add(vo);
                    }
                }
            }
        }
        lineChartVo.setTypeOne(typeOneList);
        lineChartVo.setTypeTwo(typeTwoList);
        return CommonUtils.msg(lineChartVo);
    }
}
